﻿using System.Text;

namespace FluentTagsGenerator
{
    class CtorGenerator
    {
        const string _ctorTextTemplate = @"
        public ${Class}Tag(string text) : this()
        {
            Controls.Add(new LiteralControl(text));
        }

        public ${Class}Tag(object value) : this()
        {
            Controls.Add(new LiteralControl(SafeString(value)));
        }
        ";

        const string _ctorControlTemplate = @"
        public ${Class}Tag(Control control) : this()
        {
            Controls.Add(control);
        }
        ";

        public string ClassName { get; private set; }
        public StringBuilder ClassContent { get; private set; }
        public bool IsText { get; private set; }

        public CtorGenerator(string className, StringBuilder classContent, bool isText)
        {
            ClassName = className;
            ClassContent = classContent;
            IsText = isText;
        }

        public StringBuilder Generate()
        {
            if (IsText)
            {
                ClassContent = GenerateTextMethod(_ctorTextTemplate, "${textCtor}");
            }
            else
            {
                ClassContent = GenerateTextMethod("", "${textCtor}");
            }

            ClassContent = GenerateTextMethod(_ctorControlTemplate, "${controlCtor}");

            return ClassContent;
        }

        private StringBuilder GenerateTextMethod(string template, string ToReplace)
        {
            string method = "";
            
            method =
                template.Replace("${Class}", ClassName);
            
            ClassContent = ClassContent.Replace(ToReplace, method);

            return ClassContent;
        }
        
    }
}
